package com.yinhe.music.yhmusic.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

/**
 * Created by wm on 2016/1/22.
 */
public class MyItemDecoration extends RecyclerView.ItemDecoration {

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    private int mDividerHeight = 1;
    private Drawable mDivider;
    private Context mContext;

    private int mOrientation;
    private int from;
    private int left;

    public MyItemDecoration(Context context, int orientation, int from,int left) {
        if (ATTRS != null) {
            final TypedArray a = context.obtainStyledAttributes(ATTRS);
            mDivider = a.getDrawable(0);
            mContext = context;
            this.from = from;
            this.left = left;
            a.recycle();
            setOrientation(orientation);
        } else {
            Toast.makeText(context, "request is null", Toast.LENGTH_SHORT).show();
        }
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    /**
     * 是否可以绘制分割线
     *
     * @param parent     当前的RecyclerView
     * @param itemView   当前的内容项
     * @param totalCount 适配器的item总数，可能大于RecyclerView的item总数
     * @return
     */
    private boolean isDraw(RecyclerView parent, View itemView, int totalCount) {
        int itemPosition = parent.getChildAdapterPosition(itemView);
        if (itemPosition>from) {
            return true;
        }
        return false;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }


    private void drawVertical(Canvas c, RecyclerView parent) {

        int totalCount = parent.getAdapter().getItemCount();

        //获取当前可见的item的数量，半个也算
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            //获取当前可见的view
            View child = parent.getChildAt(i);
            if (isDraw(parent, child, totalCount)) {
                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                int right = child.getRight() + params.rightMargin;
                int top = child.getTop() - mDividerHeight - params.bottomMargin;
                int bottom = top + mDividerHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
            state) {
        super.getItemOffsets(outRect, view, parent, state);
        int totalCount = parent.getAdapter().getItemCount();
        int itemPosition = parent.getChildAdapterPosition(view);
        if (isDraw(parent, view, totalCount)) {
            if (itemPosition == 0) {
                outRect.set(0, 0, 0, 0);
            } else {
                outRect.set(0, mDividerHeight, 0, 0);
            }
        }
    }
}
